package com.baseweb.webadmin.core.util;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SerialNumberUtil {
    private static final String SERIAL_NUMBER = "XXXX"; // 流水号格式
    private static SerialNumberUtil primaryGenerater = null;
    private static String unionId;
    private SerialNumberUtil(Integer unionId) {
        this.unionId =String.format("%04d",unionId);
    }
    public static SerialNumberUtil getInstance(Integer unionId) {
        if (primaryGenerater == null) {
            synchronized (SerialNumberUtil.class) {
                primaryGenerater = new SerialNumberUtil(unionId);
            }
        }
        return primaryGenerater;
    }
    public synchronized String geneterNextNumber(String sno) {
        String id = null;
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        if (sno == null) {
            id = unionId + formatter.format(date) + "0001";
        } else {
            int count = SERIAL_NUMBER.length();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < count; i++) {
                sb.append("0");
            }
            DecimalFormat df = new DecimalFormat(sb.toString());
            id = unionId+formatter.format(date)+df.format(1 + Integer.parseInt(sno.substring(12,16)));
        }
        return id;
    }
}
